package org.lanqiao.music.system.service.impl;

import org.lanqiao.music.system.dao.CommentDao;
import org.lanqiao.music.system.entity.Comment;
import org.lanqiao.music.system.service.CommentService;
import org.lanqiao.music.system.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private RedisUtils redisUtils ;
    @Value("${sys.common.code}")
    private String commonPrefix;

    @Override
    public List<Comment> getComment(Integer musicId) {
//        List<Comment> list = new ArrayList<>();
//        if (!redisUtils.hasKey(commonPrefix+":"+musicId)){
//            System.out.println("数据库查询");
//            list = commentDao.getComment(musicId);
//            for (Comment comment : list){
//                redisUtils.lSet(commonPrefix+":"+comment.getMusicId(),comment);
//            }
//        }else {
//            System.out.println("redis查询");
//            List<Object> list1 = redisUtils.lGet(commonPrefix+":"+musicId,0,-1);
//            for (Object o : list1){
//                if (o instanceof Comment){
//                    list.add((Comment) o);
//                }else {
//                    System.out.println("redis数据类型错误");
//                }
//            }
//        }
        List<Comment> list = commentDao.getComment(musicId);
        ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
        for (int index = 0; index < list.size(); index++) {
            map.put(list.get(index).getId(), index);
        }
        // 遍历寻找父节点
        for (Comment comment : list) {
            Integer pid = comment.getPid();
            // 有父节点
            if (pid != null) {
                // 获取父节点id
                Integer indexFather = map.get(pid);
                Comment father = list.get(indexFather);
                if (father.getChildren() == null) {
                    father.setChildren(new ArrayList<>());
                }
                // 在父节点上添加当前节点
                father.getChildren().add(comment);
            }
        }
        // 过滤出一级节点
        List<Comment> ans = list.stream().filter(child -> child.getPid() == null).collect(Collectors.toList());
        return ans;
    }

    @Override
    public int save(Comment comment) {
        System.out.println("保存=================");
        int result = commentDao.save(comment);
//        redisUtils.del(commonPrefix+);
//        redisUtils.lSet(commonPrefix+":"+comment.getMusicId(),co);
        return result;
    }
    @Override
    public int deleteById(Integer musicId,Integer id) {
//        redisUtils.del(commonPrefix+":"+musicId+":"+id);
        return commentDao.deleteById(musicId,id);
    }
}
